<?php 

//$_SERVER['SERVER_NAME']


function login($login)
{
	if(!isset($_SESSION['accounts']))
	{
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		echo "<script type='text/javascript'>alert('$login');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/login.html'</script>";die;
	}
	if($_SESSION['paral8'] == 0 && $_SESSION['agencylv'] ==100)
	{
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		echo "<script type='text/javascript'>alert('您好，您没有权限进入');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/login.html'</script>";die;
	}
}

function achieve($games_users,$gems2)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('您已赠送给玩家 {$games_users[0]['name']} 钻石 {$gems2} 个,已成功');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/searchplayer.html'</script>";die;
}

function error($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('$error');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/searchplayer.html'</script>";die;
}

function error_add($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('$error');history.go(-1)</script>";die;
}

function myadmin($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('$error');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/myadmin.html'</script>";die;
}

function myadmin_login($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('$error');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/login.html'</script>";die;
}

// function subordinate($error)
// {
// 	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
// 	echo "<script type='text/javascript'>alert('$error');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/subordinate.html'</script>";die;
// }

function subordinate($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('$error');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/subordinate.html'</script>";
}

function subordSuns($suns)//一键领取所有返利后
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('您有 {$suns} 个钻石下级返利，点击确定领取');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/subordinate.html'</script>";die;
}

function error_add_four($singleAgentInfo,$gems2)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('您已赠送给玩家 {$singleAgentInfo['username']} 钻石 {$gems2} 个,已成功');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/myadmin.html'</script>";die;
}

function indexx($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('$error');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/index.html'</script>";;die;
}

function policy($error)
{
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
	echo "<script type='text/javascript'>alert('$error');location.href='http://".$_SERVER['SERVER_NAME']."/zy_agent/policy.html'</script>";die;
}

function format($args, $argv) {
	$pos = 0;
	while($pos = strpos($args, '?')) {
		$arg = array_shift($argv);
		$argCnt = count($arg);
		$args = substr_replace($args, $arg, $pos, $argCnt);
	}
	return $args;
}

function get_str($model,$state = false,$sql) {
	$func = function (&$cate, $a = 0,$floor) use ($model, $state, $sql, &$func) {
		if($state == false){
		    $sql = "select * from t_agent where referrer=".$a;
		} else {
			$value = array();
			$value[] = $a;
			$sql = format($sql, $value);
		}

		//dump($sql);die;
		$result = $model->query($sql);
		if(count($result) > 0){
			$floor = $floor + 1;
			for ($i = 0; $i < count($result); $i++) {
				$result[$i]['floor']=$floor;
				array_push($cate, $result[$i]);
				$func($cate, $result[$i]['accounts'], $floor);
			}
		}
	};
	return $func;
}

function get_flo($model,$state = false,$sql) {
	$func = function (&$cate, $a = 0,$floor) use ($model, $state, $sql, &$func) {
		if($state == false){
		    $sql = "select * from t_agent where referrer=".$a;
		} else {
			$value = array();
			$value[] = $a;
			$sql = format($sql, $value);
		}

		//dump($sql);die;
		$result = $model->query($sql);
		if(count($result) > 0){
			$floor = $floor + 1;
			for ($i = 0; $i < count($result); $i++) {
				$result[$i]['floor']=$floor;
				array_push($cate, $result[$i]);
				$func($cate, $result[$i]['accounts'], $floor);
			}
		}
	};
	return $func;
}

function get_age($model, $sqls) {//查看数据
			
    	$result = array();
  		foreach ($sqls as $key => $sql) {
  			$result[] = $model->query($sql);
  		}
       //dump($result);die;
       return $result;
      
}

function get_Retained($model, $sets) {//修改数据
			
    	$result = array();
  		foreach ($sets as $key => $set) {
  			$result[] = $model->execute($set);
  		}
       //dump($result);die;
       return $result;
      
}




function get_bate($model,$state = false,$sql) {//下级找上级找到总代理为止
	$func = function (&$cate, &$a = 0, &$count) use ($model, $state, $sql, &$func) {
		if($state == false){
		    $sql = "select * from t_agent where accounts=".$a;
		} else {
			$value = array();
			$value[] = $a;
			$sql = format($sql, $value);
		}
		
		// dump($sql);
		$result = $model->query($sql);
		if(count($result) > 0){
			 //dump($result);
			if ($result[0]['agencylv'] != 100) {
				$count = $count + 1;
				$cate = $result;
				$func($cate, $result[0]['referrer'], $count);
			}
		}
	};
	return $func;
}

function sortArrayDesc($preData,$sortType){    //二维数组排序函数
    $sortData = array();    
    foreach ($preData as $key_i => $value_i){    
        $price_i = $value_i[$sortType];    
        $min_key = '';    
        $sort_total = count($sortData);    
        foreach ($sortData as $key_j => $value_j){    
            if($price_i>$value_j[$sortType]){    
                $min_key = $key_j+1;    
                break;    
            }    
        }    
        if(empty($min_key)){    
            array_push($sortData, $value_i);     
        }else {    
            $sortData1 = array_slice($sortData, 0,$min_key-1);     
            array_push($sortData1, $value_i);    
            if(($min_key-1)<$sort_total){    
                $sortData2 = array_slice($sortData, $min_key-1);     
                foreach ($sortData2 as $value){    
                    array_push($sortData1, $value);    
                }    
            }    
            $sortData = $sortData1;    
        }    
    }    
    return $sortData;    
}    



?>